// 全局请求封装
const base_url = 'http://braincognitiontec.top:30003'

// 需要修改token，和根据实际修改请求头
const request = (url, method, params) => {
	// 获取本地token
	const token = uni.getStorageSync('Token');

	//判断进入页面的时候或者本地有没有token,有就直接值某一页，没有则请求接口
	// if (token || url == "api/User/Login") {
	if (url != 'api/EnergyCalculation/GetReportImg' && url != 'api/EnergyCalculation/GetThreeModelImg' && url !=
		'api/EnergyCalculation/DepthReportCalculation') {
		uni.showLoading({
			title: "加载中"
		});
	}
	// 根据请求方法或URL来判断是否添加请求头
	let header = {}
	if (method == "post") {
		header.token = token
		header['content-type'] = "application/json"
	} else if (url == "api/WxUser/GetWxUser") {
		// header['content-type'] = "application/json"
		header = {
			token: token
		}
	} else {
		header = {
			token: token
		}
	}
	console.log(header)


	return new Promise((resolve, reject) => {
		console.log(base_url + url)
		uni.request({
			//url: "http://192.168.1.15:6001/" + url,
			url: base_url + url,
			method: method,
			header: header,
			data: params,
			success(response) {
				uni.hideLoading();
				const res = response
				console.log('res', response)
				// console.log(res)
				// 根据返回的状态码做出对应的操作
				if (res.statusCode == '200' && res.data.success) {
					console.log(res)
					resolve(res);
				} else if (res.statusCode == '501') {
					uni.clearStorageSync()
					uni.showToast({
						title: res.data.errorMsg,
						icon: 'none'

					})
				} else {
					resolve(res);
					uni.showToast({
						title: res.data.errorMsg,
						icon: 'none'
					})
				}
			},
			fail(err) {
				console.log(err)
				uni.showToast({
					title: err.errMsg,
					icon: 'none'
				})
				reject(err);
			},
			complete() {
				uni.hideLoading();
			}
		});
	}).catch((e) => {});
	// }else{
	// 	uni.switchTab({
	// 		url: '/pages/ucenter/login-page/pwd-login/pwd-login'
	// 	});
	// }
};

export default request